/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JServiceCalendarWeakLineEditor.java
 *
 * Created on 2010-10-4, 16:47:25
 */

package cn.ekuma.epos.servings;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;

import com.openbravo.bean.ServiceCalendarWeakLine;
import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import java.util.Date;
import java.util.Properties;

/**
 *
 * @author Administrator
 */
public class JServiceCalendarWeakLineEditor  extends AbstractChildDAOJEditor<ServiceCalendarWeakLine,String> {
    ComboBoxValModel dayOfweakModel;

    public JServiceCalendarWeakLineEditor(DirtyManager dirty) {
       super(dirty);
       initComponents();
        jTxtStartDate.getDocument().addDocumentListener(dirty);
        jTxtEndDate.getDocument().addDocumentListener(dirty);
        jComboBox1.addActionListener(dirty);
        dayOfweakModel=new ComboBoxValModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jTxtEndDate = new javax.swing.JTextField();
        btnDateEnd = new javax.swing.JButton();
        btnDateStart = new javax.swing.JButton();
        jTxtStartDate = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        jLabel4.setText(AppLocal.getIntString("Label.EndDate")); // NOI18N

        jTxtEndDate.setPreferredSize(new java.awt.Dimension(150, 22));

        btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateEnd.setFocusable(false);
        btnDateEnd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDateEnd.setPreferredSize(new java.awt.Dimension(55, 25));
        btnDateEnd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateEndActionPerformed(evt);
            }
        });

        btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
        btnDateStart.setFocusable(false);
        btnDateStart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnDateStart.setPreferredSize(new java.awt.Dimension(55, 25));
        btnDateStart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnDateStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDateStartActionPerformed(evt);
            }
        });

        jTxtStartDate.setMinimumSize(new java.awt.Dimension(6, 15));
        jTxtStartDate.setPreferredSize(new java.awt.Dimension(150, 22));

        jLabel3.setText(AppLocal.getIntString("Label.StartDate")); // NOI18N

        jLabel1.setText(AppLocal.getIntString("Label.service.dayofweek")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTxtEndDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTxtStartDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel1)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel3)
                            .addComponent(jTxtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel4)
                            .addComponent(jTxtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(btnDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(btnDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(44, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateEndActionPerformed

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateStartActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDateEnd;
    private javax.swing.JButton btnDateStart;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTxtEndDate;
    private javax.swing.JTextField jTxtStartDate;
    // End of variables declaration//GEN-END:variables

    @Override
    public ServiceCalendarWeakLine createNew(Properties prop) {
        ServiceCalendarWeakLine obj=new ServiceCalendarWeakLine();
        obj.setServiceCalenderId(parentNode.getParentKey().getKey());
        return obj;
    }

    @Override
    public void rebuild(ServiceCalendarWeakLine obj) throws BasicException {
        obj.setDayInWeak((Integer)dayOfweakModel.getSelectedKey());
        obj.setStart((Date)Formats.TIME.parseValue(jTxtStartDate.getText()));
        obj.setEnd((Date)Formats.TIME.parseValue(jTxtEndDate.getText()));
    }

    @Override
    public void updateUI(boolean editAble, int state) {
        dayOfweakModel.setSelectedKey(new Integer(obj.getDayInWeak()));
        jTxtStartDate.setText(Formats.TIME.formatValue(obj.getStart()));
        jTxtEndDate.setText(Formats.TIME.formatValue(obj.getStart()));

        jComboBox1.setEditable(editAble);
        jTxtStartDate.setEditable(editAble);
        jTxtEndDate.setEditable(editAble);
        btnDateStart.setEnabled(editAble);
        btnDateEnd.setEnabled(editAble);
    }

    public void refresh() {
        
    }

    @Override
    public String getTabName() {
       return "Weak Line";
    }

    @Override
    public Class getEditClass() {
        return ServiceCalendarWeakLine.class;
    }

}
